# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

import os
from scrapy.pipelines.images import ImagesPipeline
import requests
from . import settings

class PhotoPipeline(object):
    def __init__(self):
        self.path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'images')
        if not os.path.exists(self.path):
            os.mkdir(self.path)

    def process_item(self, item, spider):
        categroy=item['title']
        categroy_path=os.path.join(self.path,categroy)
        if not os.path.exists(categroy_path):
            os.mkdir(categroy_path)
        for img in item['img_urls']:
            img_name=img.split('_')[-1]
            content=requests.get(img).content
            img_path=os.path.join(categroy_path,img_name)
            with open(img_path,'wb') as f:
                f.write(content)
        return item

#   自己改编下载路径用自己的定义的路径的类写不出来啊！太耻辱了
class PhotoImagesPipline(ImagesPipeline):
    def get_media_requests(self, item, info):
        #返回图片链接的列表
        request_objs=super(PhotoImagesPipline,self).get_media_requests(item,info)
        for request_obj in request_objs:
            request_obj.item=item
        return request_objs

    def file_path(self, request, response=None, info=None):
        path=super(PhotoImagesPipline,self).file_path(request,response,info)
        title=request.item.get('title')
        ImageStore=settings.IMAGES_STORE
        img_path=os.path.join(ImageStore,title)
        if not os.path.exists(img_path):
            os.mkdir(img_path)
        img_name=path.replace('full/','')
        img_full_path=os.path.join(img_path,img_name)
        #print(img_full_path)
        return img_full_path
